package com.zsck.pos.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.lifecycle.Observer;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chinaums.commoncheck.R;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.zsck.pos.bean.FeeEvent;
import com.zsck.pos.bean.FeeVOListBean;
import com.zsck.pos.bean.MonthBill;
import com.zsck.pos.bean.SecondLiveEvent;
import com.zsck.pos.bean.ThreadLiveEvent;
import com.zsck.pos.utils.LogUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class SecondMonthAdapter extends BaseNodeProvider  {

    public SecondMonthAdapter() {

    }

    @Override
    public int getItemViewType() {
        return 2;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_second;
    }


    @Override
    public void convert(@NotNull BaseViewHolder baseViewHolder, BaseNode baseNode) {
        MonthBill entity = (MonthBill) baseNode;
        baseViewHolder.setText(R.id.tv_month, entity.getMonth())
                .setText(R.id.tv_month, entity.getMonth())
                .setText(R.id.month_amount, Double.toString(entity.getAmount()));
        //首月和尾月账单处理
        if(TextUtils.equals("1", entity.getBillType())||TextUtils.equals("returnRoom", entity.getSource()) || TextUtils.equals("returnRent", entity.getSource())){
            baseViewHolder.setVisible(R.id.iv_arrow,false);
            if (TextUtils.equals("1", entity.getBillType())) {
                baseViewHolder.setText(R.id.tv_sub, "首月账单").setVisible(R.id.tv_sub,true);
            }
            if (TextUtils.equals("returnRoom", entity.getSource()) || TextUtils.equals("returnRent", entity.getSource())) {
                baseViewHolder.setVisible(R.id.tv_sub, true);
                baseViewHolder.setText(R.id.tv_sub, "尾月账单");
            }
        }else{
            baseViewHolder.setVisible(R.id.tv_sub, false);
        }
        //箭头指示
        if(entity.isExpanded()){
            baseViewHolder.setImageResource(R.id.iv_arrow, R.mipmap.arror_down);
        }else{
            baseViewHolder.setImageResource(R.id.iv_arrow, R.mipmap.icon_arror_right);
        }

        CheckBox secondCheck = baseViewHolder.getView(R.id.second_check);
        addChildClickViewIds(R.id.second_check);
        secondCheck.setOnCheckedChangeListener(null);
        secondCheck.setChecked(entity.isChecked());
        if(entity.isChecked()){
            secondCheck.setChecked(true);
        }else{
            secondCheck.setChecked(false);
        }
        LiveEventBus.get(ThreadLiveEvent.class)
                .observeForever(new Observer<ThreadLiveEvent>() {

                    @Override
                    public void onChanged(ThreadLiveEvent multiBillsEvent) {
                        FeeVOListBean feeVOListBean = multiBillsEvent.getFeeVOListBean();
                        List<FeeVOListBean> feeVOList = entity.getFeeVOList();
                        for(int i =0;i<feeVOList.size();i++){
                            FeeVOListBean feeVOListBeans = feeVOList.get(i);

                            if(TextUtils.equals(feeVOListBeans.getFeeId(),feeVOListBean.getFeeId())){
                                feeVOListBeans=feeVOListBean;
                            }
                            if(feeVOListBeans.isChecked()){
                                entity.setChecked(true);
                                break;
                            }
                            if(i==feeVOList.size()-1){
                                entity.setChecked(false);
                            }

                        }
                        getAdapter().nodeReplaceChildData(entity, feeVOList);
                        int itemPosition = getAdapter().getItemPosition(entity);
                        getAdapter().notifyItemChanged(itemPosition);
                    }
                });
        secondCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                List<FeeVOListBean> feeVOList = entity.getFeeVOList();
                int itemPosition = getAdapter().getItemPosition(entity);
                entity.setChecked(isChecked);

                if (isChecked) {
                    LiveEventBus.get(SecondLiveEvent.class)
                            .post(new SecondLiveEvent(entity, itemPosition));
                    List<FeeVOListBean> newList = new ArrayList<>();
                    for (FeeVOListBean feeVOListBean : feeVOList) {
                        if (feeVOListBean.isChecked()) {
                            return;
                        } else {
                            feeVOListBean.setChecked(true);
                            LiveEventBus.get(FeeEvent.class)
                                    .post(new FeeEvent(feeVOListBean, true));
                        }
                        newList.add(feeVOListBean);
                        //刷新数据
                        getAdapter().nodeReplaceChildData(entity, newList);
                    }

                } else {
                    LiveEventBus.get(SecondLiveEvent.class)
                            .post(new SecondLiveEvent(entity, itemPosition));
                    List<FeeVOListBean> newList = new ArrayList<>();
                    for (FeeVOListBean feeVOListBean : feeVOList) {
                        if (feeVOListBean.isChecked() == false) {
                            return;
                        } else {
                            feeVOListBean.setChecked(false);
                            LiveEventBus.get(FeeEvent.class)
                                    .post(new FeeEvent(feeVOListBean, false));
                        }
                        newList.add(feeVOListBean);
                        //刷新数据
                        getAdapter().nodeReplaceChildData(entity, newList);
                    }

                }
            }
        });
    }


    @Override
    public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
        MonthBill entity = (MonthBill) data;
        if(TextUtils.equals("1", entity.getBillType())||TextUtils.equals("returnRoom", entity.getSource()) || TextUtils.equals("returnRent", entity.getSource())){
            return;
        }

        if (entity.isExpanded()) {
            helper.setImageResource(R.id.iv_arrow, R.mipmap.arror_down);
            getAdapter().collapse(position);
        } else {
            helper.setImageResource(R.id.iv_arrow, R.mipmap.icon_arror_right);
            getAdapter().expand(position);
        }
    }

}
